# database.py
from flask import Flask
from models import db, Category, Product

def init_db(app: Flask):
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///instance/shopping.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)

    with app.app_context():
        db.create_all()
        
        # 添加示例商品（带库存）
        if Product.query.count() == 0:
            products = [
                Product(name="iPhone 15", price=6999, stock=5, image="static/uploads/iphone.jpg"),
                Product(name="MacBook Pro", price=12999, stock=3, image="static/uploads/macbook.jpg"),
                Product(name="AirPods Pro", price=1999, stock=0, image="static/uploads/airpods.jpg"),  # 售罄
                Product(name="iPad Air", price=4399, stock=8, image="static/uploads/ipad.jpg"),
            ]
            for p in products:
                db.session.add(p)
            db.session.commit()
            print("✅ 示例商品（含库存）已添加")


# -------------------------- 初始化测试数据（首次运行用）--------------------------
def init_test_data(app: Flask):

    # 初始化数据库
    db.init_app(app)

    with app.app_context():
        # 清空现有数据（谨慎：生产环境注释）
        db.drop_all()
        db.create_all()

        # 创建分类
        categories = [
            Category(name="电子产品", slug="electronics"),
            Category(name="家居用品", slug="home-goods"),
            Category(name="服装鞋帽", slug="clothing"),
            Category(name="食品饮料", slug="food")
        ]
        db.session.add_all(categories)
        db.session.commit()

        # 创建商品（关联分类，含库存信息）
        products = [
            # 电子产品（库存充足）
            Product(
                name="超薄全面屏智能手机",
                description="6.7英寸超清大屏，5000mAh电池，128GB存储",
                price=3299.0,
                stock=256,  # 库存充足
                image_url="uploads/iphone.jpg",
                rating=4.8,
                review_count=128,
                category_id=categories[0].id
            ),
            # 家居用品（库存紧张）
            Product(
                name="北欧风格三人沙发",
                description="简约现代设计，高密度海绵，灰色亚麻面料",
                price=2799.0,
                stock=12,  # 库存紧张
                image_url="uploads/ipad.jpg",
                rating=4.6,
                review_count=42,
                category_id=categories[1].id
            ),
            # 服装（库存充足）
            Product(
                name="男士纯棉休闲衬衫",
                description="100%纯棉，修身版型，白色基础款",
                price=199.0,
                stock=124,
                image_url="uploads/macbook.jpg",
                rating=4.9,
                review_count=215,
                category_id=categories[2].id
            ),
            # 食品（库存不足）
            Product(
                name="进口黑巧克力礼盒",
                description="85%可可含量，纯可可脂，礼盒装",
                price=128.0,
                stock=5,  # 库存不足
                image_url="uploads/placeholder.jpg",
                rating=4.7,
                review_count=326,
                category_id=categories[3].id
            )
        ]
        db.session.add_all(products)
        db.session.commit()
        print("测试数据初始化完成！")
